﻿using System;
using System.IO;


namespace FrameDetectionTest
{
    public static class Log
    {
        public enum LogType
        {
            workmessage,
            mes,
            cim,
            cv,
            record,
            error
        }

        public static void Print(string msg)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            try
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("[" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + "] " + msg);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (IOException e)
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("[" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + "]" + e.Message);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        } // end Print


        public static void Print(string msg, LogType ly)
        {
            string LogBaseDirectory = Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory) + "FrameDetectionTestLog";
            if (!Directory.Exists(LogBaseDirectory))
            {
                Directory.CreateDirectory(LogBaseDirectory);
            }

            string LogDirectory = string.Empty;
            LogDirectory = LogBaseDirectory + "\\WorkMessage";
            if (!Directory.Exists(LogDirectory))
            {
                Directory.CreateDirectory(LogDirectory);
            }

            LogDirectory = LogBaseDirectory + "\\MES";
            if (!Directory.Exists(LogDirectory))
            {
                Directory.CreateDirectory(LogDirectory);
            }

            LogDirectory = LogBaseDirectory + "\\CIM";
            if (!Directory.Exists(LogDirectory))
            {
                Directory.CreateDirectory(LogDirectory);
            }

            LogDirectory = LogBaseDirectory + "\\Record";
            if (!Directory.Exists(LogDirectory))
            {
                Directory.CreateDirectory(LogDirectory);
            }

            LogDirectory = LogBaseDirectory + "\\Error";
            if (!Directory.Exists(LogDirectory))
            {
                Directory.CreateDirectory(LogDirectory);
            }

            LogDirectory = LogBaseDirectory + "\\Cv";
            if (!Directory.Exists(LogDirectory))
            {
                Directory.CreateDirectory(LogDirectory);
            }

            string logPath = string.Empty;
            switch (ly)
            {
                case LogType.workmessage:
                    logPath = LogBaseDirectory + "\\WorkMessage\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    break;
                case LogType.mes:
                    logPath = LogBaseDirectory + "\\MES\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    break;
                case LogType.record:
                    logPath = LogBaseDirectory + "\\Record\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    break;
                case LogType.error:
                    logPath = LogBaseDirectory + "\\Error\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    break;
                case LogType.cim:
                    logPath = LogBaseDirectory + "\\CIM\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    break;
                case LogType.cv:
                    logPath = LogBaseDirectory + "\\Cv\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    break;
            }

            try
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("[" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + "] " + msg);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (IOException e)
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("[" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss") + "] " + e.Message);
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        } // end Print
        public static void CsvPrint(string Sn,bool Flag)
        {
            string LogBaseDirectory = Path.GetPathRoot(AppDomain.CurrentDomain.BaseDirectory) + "FrameDetectionTestLog\\" + "Cv\\";
            if (!Directory.Exists(LogBaseDirectory))
            {
                Directory.CreateDirectory(LogBaseDirectory);
            }
            string FilePath = LogBaseDirectory + DateTime.Now.ToString("yyyy-MM-dd") + ".csv";
            if (!File.Exists(FilePath))
            {
                FileStream fs = File.Create(FilePath);
                fs.Flush();
                fs.Close();
                fs.Dispose();
            }
            StreamWriter sw = new StreamWriter(FilePath, true);
            sw.WriteLine(DateTime.Now.ToString("T") + "," + Sn + "," + Flag.ToString());
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }
    }
}
